/*****************************************************************************
* Keppe Messenger (KMP) Keppe Project-Team User Messaging System
* Copyright (C) 2013-2014 Keppe Project-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

#include "frmMenu.h"
#include "frmLogin.h"
#include "frmProfile.h"
#include "ui_frmProfile.h"
#include <QDesktopWidget>
#include <QMessageBox>
QObject *formPE;
QObject *mmPE;

frmProfile::frmProfile(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::frmProfile)
{
    setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint);
    ui->setupUi(this);
}

frmProfile::~frmProfile()
{
    delete ui;
}

void frmProfile::setForm(frmLogin* login)
{
    formPE = login;
}

void frmProfile::setMenu(frmMenu* menu)
{
    mmPE = menu;
}

void frmProfile::setData(QString account, QString nick, QString message, QString contacts)
{
    ui->labName->setText("Account: " + account);
    ui->txtNick->setText(nick);
    ui->txtMsgState->setText(QString::fromUtf8(fromMessage(message)));
    ui->labContacts->setText("Contacts: " + contacts);
}

void frmProfile::on_cmdAbort_clicked()
{
    this->close();
}

void frmProfile::on_cmdOK_clicked()
{
    QString nick = ui->txtNick->text();
    QString msgstate = convertMessage(ui->txtMsgState->text().toUtf8());
    if (nick.length() >= 3)
    {
        if (nick.trimmed() == nick)
        {
            if (nick.contains("\n") || nick.contains("\r"))
            {

                QMessageBox::critical(this,"Keppe Messenger", "The new nick contains CR or LF in binary form!");

                return;
            }
        }
        else
        {

            QMessageBox::critical(this,"Keppe Messenger", "The new nick is not the same in trimmed and not trimmed form!");

            return;
        }
        if (QString::fromUtf8(nick.toUtf8()) != QString::fromLatin1(nick.toLatin1()))
        {

            QMessageBox::critical(this,"Keppe Messenger", "The new nick is not \"UTF-8 without BOM\" compatible!");

            return;
        }
        if (QString::fromUtf8(nick.toUtf8()) != QString::fromLatin1(nick.toLatin1()))
        {

            QMessageBox::critical(this,"Keppe Messenger", "The new nick is not \"UTF-8 without BOM\" compatible!");

            return;
        }
    }
    else
    {

        QMessageBox::critical(this,"Keppe Messenger", "The new nick is shorter than 3 bytes!");

        return;
    }
    if (msgstate.trimmed() == msgstate)
    {
        if (msgstate.contains("\n") || msgstate.contains("\r") || msgstate.contains("<br>"))
        {

            QMessageBox::critical(this,"Keppe Messenger", "The new message state contains CR or LF in binary form!");

            return;
        }
    }
    else
    {

        QMessageBox::critical(this,"Keppe Messenger", "The new message state is not the same in trimmed and not trimmed form!");

        return;
    }
    if (QString::fromUtf8(msgstate.toUtf8()) != QString::fromLatin1(msgstate.toLatin1()))
    {

        QMessageBox::critical(this,"Keppe Messenger", "The new message state is not \"UTF-8 without BOM\" compatible!");

        return;
    }
    if (QString::fromUtf8(msgstate.toUtf8()) != QString::fromUtf8(msgstate.toLatin1()))
    {

        QMessageBox::critical(this,"Keppe Messenger", "The new message state is not \"UTF-8 without BOM\" compatible!");

        return;
    }
    frmMenu *menu = (frmMenu*)mmPE;
    frmLogin *login = (frmLogin*)formPE;
    int retn = login->setUserNick(nick);
    if (retn != 200)
    {

        QMessageBox::critical(this,"Keppe Messenger","Name could not be changed with Error " + QString::number(retn) + "!");

        return;
    }
    menu->setNick(nick);
    login->updateWelcomeLabel(nick);
    int retm = login->setUserMessageState(msgstate);
    if (retm != 200)
    {

        QMessageBox::critical(this,"Keppe Messenger","Status could not be changed with Error " + QString::number(retm) + "!");

        return;
    }
    menu->setMessage(msgstate);
    this->close();
}

QString frmProfile::convertMessage(QByteArray message)
{
    QString text = QString::fromUtf8(message);
    text.replace(QString::fromUtf8("&"),"&amp;");
    text.replace(QString::fromUtf8("<"),"&lt;");
    text.replace(QString::fromUtf8(">"),"&gt;");
    text.replace(QString::fromUtf8("\""),"&quot;");
    text.replace(QString::fromUtf8("ä"),"&auml;");
    text.replace(QString::fromUtf8("ö"),"&ouml;");
    text.replace(QString::fromUtf8("ü"),"&uuml;");
    text.replace(QString::fromUtf8("Ä"),"&Auml;");
    text.replace(QString::fromUtf8("Ö"),"&Ouml;");
    text.replace(QString::fromUtf8("Ü"),"&Uuml;");
    text.replace(QString::fromUtf8("ß"),"&szlig;");
    text.replace(QString::fromUtf8("€"),"&euro;");
    text.replace(QString::fromUtf8("§"),"&sect;");
    text.replace(QString::fromUtf8("©"),"&copy;");
    text.replace(QString::fromUtf8("®"),"&reg;");
    text.replace(QString::fromUtf8("°"),"&deg;");
    text.replace(QString::fromUtf8("²"),"&sup2;");
    text.replace(QString::fromUtf8("³"),"&sup3;");
    text.replace(QString::fromUtf8("\r\n"),"<br>");
    text.replace(QString::fromUtf8("\r"),"<br>");
    text.replace(QString::fromUtf8("\n"),"<br>");
    return text;
}

QByteArray frmProfile::fromMessage(QString message)
{
    QByteArray text = message.toUtf8();
    text.replace("&auml;","ä");
    text.replace("&ouml;","ö");
    text.replace("&uuml;","ü");
    text.replace("&Auml;","Ä");
    text.replace("&Ouml;","Ö");
    text.replace("&Uuml;","Ü");
    text.replace("&szlig;","ß");
    text.replace("&euro;","€");
    text.replace("&sect;","§");
    text.replace("&copy;","©");
    text.replace("&reg;","®");
    text.replace("&deg;","°");
    text.replace("&sup2;","²");
    text.replace("&sup3;","³");
    text.replace("<br>","\n");
    text.replace("&quot;","\"");
    text.replace("&lt;","<");
    text.replace("&gt;",">");
    text.replace("&amp;","&");
    return text;
}
